package com.example.fw;

import java.util.logging.Logger;

import javax.swing.JDialog;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JLabelOperator;

public class HelperBase {
	
	protected final ApplicationManager manager;
	protected Logger log = Logger.getLogger("FW");
	
	public HelperBase(ApplicationManager manager) {
		this.manager = manager;	
	}
	
	protected String waitMessageDialog(String title, int timeout){
		log.info("Check message dialog");
		Long start = System.currentTimeMillis();
		Long currentTime = start;
		while (currentTime < start + timeout){
			JDialog dialog = JDialogOperator.findJDialog(manager.getApplication().getOwner(), title, false, false);
			if (dialog != null){
				JDialogOperator dialogOp = new JDialogOperator(dialog);
				String message = new JLabelOperator(dialogOp).getText();
				dialogOp.requestClose();
				log.warning(message);
				return message;
			}
			try {
				Thread.sleep(500);
			} catch (Exception e) {
				e.printStackTrace();				
			}
			currentTime = System.currentTimeMillis();
		}
		return null;
	}
	
	public void pushButton(String button) {
		JDialogOperator dialog = new JDialogOperator(manager.getApplication());
		new JButtonOperator(dialog, button).push();
	}
}
